<?php

namespace App\Repositories;

use App\Models\Product;

/**
 * @auther 商品规格属性
 */

class ProductRepository
{

    // 列表
    public function list($where = [])
    {
        return Product::with('proGg')->where($where)
            ->orderBy('created_at', 'desc')->orderBy('sort', 'desc')->get();
    }

    // 列表
    public function whereInlist($where = [])
    {
        return Product::with('proGg')->whereIn($where['id'], $where['arr'])
            ->orderBy('created_at', 'desc')->orderBy('sort', 'desc')->get();
    }
    // 分页列表
    public function pageList($where = [])
    {
        !isset($where['page']) ? $page = 15 : $page = $where['page'];

        return $userList = Product::orderBy('created_at', 'desc')->orderBy('sort', 'desc')->where(function ($query) use ($where) {
            if (isset($where['search'])) {
                $query->where('name', 'like', '%' . $where['search'] . '%');
            }
            if (isset($where['category'])) {
                $query->where('category', $where['category']);
            }
            if (isset($where['tc'])) {
                $query->where('tc', $where['tc']);
            }
            if (isset($where['zixuan'])) {
                $query->where('zixuan', $where['zixuan']);
            }
        })->paginate($page);
    }

    // add
    public function create($param)
    {
        $sys = new Product();
        $sys->fill($param)->save();
        return $sys->id;
    }

    // update
    public function update($param)
    {
        $sys = Product::find($param['id']);
        return $sys->fill($param)->save();
    }

    // del
    public function delete($id)
    {
        return Product::where('id', $id)->delete();
    }

    // info
    public function info($param)
    {
        return Product::with('proGg')->where($param)->first();
    }
    public function stateUpdate($param)
    {
        $sys = Product::whereIn('id',$param['ids']);
        // var_dump($sys);die();
        unset($param['ids']);
        return $sys->update($param);   
    }

}
